package com.example.userlogincenter.userCenter.service.impl;


import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTHeader;
import cn.hutool.jwt.JWTUtil;
import com.example.userlogincenter.userCenter.entity.CenterUser;
import com.example.userlogincenter.userCenter.service.ICenterUserService;
import com.example.userlogincenter.userCenter.service.KeyService;
import com.example.userlogincenter.userCenter.util.JWTUtils;
import com.example.userlogincenter.userCenter.util.randomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class KeyServiceImpl implements KeyService {

    @Autowired
    private ICenterUserService userService;

    /**
     *
     * 更新KeySalt，重新生成KeySign
     *
     * **/
    @Override
    public boolean updateKey(String userId) {
        String keySalt = randomUtils.randomAccess();
        CenterUser currentUser = userService.getById(userId);
        currentUser.setKeySalt(keySalt);
        //        重新分配签名
        String keySign = JWTUtils.generateApiSign(keySalt);
        currentUser.setAsscessSecret(keySign);
        boolean isUpdate = userService.updateById(currentUser);
        if (isUpdate){
            return true;
        }
        return false;
    }
}
